﻿using MotionControl.Modules.LightSource.LightControl;
using Newtonsoft.Json;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Media.Media3D;

namespace MotionControl.Modules.LightSource.Models
{
    [JsonObject(MemberSerialization.OptIn)]
    public class Channel: INotifyPropertyChanged
    {
        public Channel(int number, int intensity, string name) {
            Number = number;
            Intensity = intensity;
            Name = name;
            OpenCloseCmd = new DelegateCommand(OnOpenClose);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public Action<int, bool> SwitchingChanged;
        public Action<int, int> IntensityChanged;
        public DelegateCommand OpenCloseCmd { get; set; }

        [JsonProperty]
        public int Number { get; set; }

        private int _intensity = 110;
        [JsonProperty]
        public int Intensity
        { 
            get { return _intensity; }
            set {
                if (_intensity == value)
                    return;
                _intensity = value;
                OnPropertyChanged("Intensity");
                IntensityChanged?.Invoke(Number, value);                            
            }
        }

        [JsonProperty]
        public string Name { get; set; }

        private bool _isOpen = true;
        public bool IsOpen
        {
            get { return _isOpen; }
            set
            {
                if (_isOpen == value)
                    return;
                _isOpen = value;
                OnPropertyChanged("IsOpen");         
                if (SwitchingChanged != null)
                {
                    SwitchingChanged?.Invoke(Number, value);
                }
            }
        }
        void OnOpenClose()
        {
            IsOpen = !IsOpen;
        }

        private void OnPropertyChanged(string info)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(info));
        }
    }
}
